package cn.iocoder.yudao.module.mini.controller.admin.healthy;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.mini.controller.admin.healthy.vo.*;
import cn.iocoder.yudao.module.mini.dal.dataobject.healthy.HealthyDO;
import cn.iocoder.yudao.module.mini.convert.healthy.HealthyConvert;
import cn.iocoder.yudao.module.mini.service.healthy.HealthyService;

@Tag(name = "管理后台 - 技师健康档案")
@RestController
@RequestMapping("/mini/healthy")
@Validated
public class HealthyController {

    @Resource
    private HealthyService healthyService;

    @PostMapping("/create")
    @Operation(summary = "创建技师健康档案")
    @PreAuthorize("@ss.hasPermission('mini:healthy:create')")
    public CommonResult<Long> createHealthy(@Valid @RequestBody HealthyCreateReqVO createReqVO) {
        return success(healthyService.createHealthy(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新技师健康档案")
    @PreAuthorize("@ss.hasPermission('mini:healthy:update')")
    public CommonResult<Boolean> updateHealthy(@Valid @RequestBody HealthyUpdateReqVO updateReqVO) {
        healthyService.updateHealthy(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除技师健康档案")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mini:healthy:delete')")
    public CommonResult<Boolean> deleteHealthy(@RequestParam("id") Long id) {
        healthyService.deleteHealthy(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得技师健康档案")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mini:healthy:query')")
    public CommonResult<HealthyRespVO> getHealthy(@RequestParam("id") Long id) {
        HealthyDO healthy = healthyService.getHealthy(id);
        return success(HealthyConvert.INSTANCE.convert(healthy));
    }

    @GetMapping("/list")
    @Operation(summary = "获得技师健康档案列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('mini:healthy:query')")
    public CommonResult<List<HealthyRespVO>> getHealthyList(@RequestParam("ids") Collection<Long> ids) {
        List<HealthyDO> list = healthyService.getHealthyList(ids);
        return success(HealthyConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得技师健康档案分页")
    @PreAuthorize("@ss.hasPermission('mini:healthy:query')")
    public CommonResult<PageResult<HealthyRespVO>> getHealthyPage(@Valid HealthyPageReqVO pageVO) {
        PageResult<HealthyDO> pageResult = healthyService.getHealthyPage(pageVO);
        return success(HealthyConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出技师健康档案 Excel")
    @PreAuthorize("@ss.hasPermission('mini:healthy:export')")
    @OperateLog(type = EXPORT)
    public void exportHealthyExcel(@Valid HealthyExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<HealthyDO> list = healthyService.getHealthyList(exportReqVO);
        // 导出 Excel
        List<HealthyExcelVO> datas = HealthyConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "技师健康档案.xls", "数据", HealthyExcelVO.class, datas);
    }

}
